<?php

/**
 * 官方文件模型
 */
class Document_model extends CI_Model {
    
    const TN = "documents";
    
    public function insert($document) {
        $this->db->insert(self::TN, $document);
    }
    
    /**
     * 按relation删除
     * @param type $rid
     */
    public function delete_batch_by_relation($relation_id) {
        $this->db->delete(self::TN, array('relation_id' => $relation_id));
    }
    
    /**
     * 
     * @param type $conditions
     * @param type $per_page 默认0表示取所有记录
     * @param type $page
     * @return array
     */
    public function fetch_array($conditions, $per_page = 0, $page = 0) {
        $this->_set_where($conditions);
        if ($per_page > 0) {
            $offset = ($page - 1) * $per_page;
            $this->db->limit($per_page, $offset);
        }

        return $this->db->get(self::TN)->result_array();
    }
    
    /**
     * 设置where
     * @param type $conditions
     */
    private function _set_where($conditions) {
        if (isset($conditions['relation_id'])) {
            $this->db->where('relation_id', $conditions['relation_id']);
        }
    }
}

